package org.opencv;

import android.content.Context;
import android.util.Log;
import org.greenrobot.eventbus.EventBus;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class OpenCVSDK {
    private static final String TAG = "OpenCVSDK";
    private static final OpenCVSDK ourInstance = new OpenCVSDK();

    public static OpenCVSDK getInstance() {
        return ourInstance;
    }

    private OpenCVSDK() {
    }

    public void run(Context context, String str, String str2, String str3, int i) {
        Exception e;
        try {
            Mat imread = Imgcodecs.imread(str);
            Mat imread2 = Imgcodecs.imread(str2);
            System.currentTimeMillis();
            for (int i2 = 0; i2 < i; i2++) {
                try {
                    Mat mat = new Mat((imread.rows() - imread2.rows()) + 1, (imread.cols() - imread2.cols()) + 1, CvType.CV_32FC1);
                    Imgproc.matchTemplate(imread, imread2, mat, 3);
                    double d = 0.0d;
                    for (int i3 = 0; i3 < mat.rows(); i3++) {
                        for (int i4 = 0; i4 < mat.cols(); i4++) {
                            double d2 = mat.get(i3, i4)[0];
                            if (d < d2) {
                                d = d2;
                            }
                        }
                    }
                    int i5 = (int) (d * 100.0d);
                    int picLike = getPicLike(context);
                    Core.normalize(mat, mat, 0.0d, 1.0d, 32, -1, new Mat());
                    Point point = Core.minMaxLoc(mat).maxLoc;
                    Imgproc.rectangle(imread, point, new Point(point.x + ((double) imread2.cols()), point.y + ((double) imread2.rows())), new Scalar(0.0d, 255.0d, 0.0d));
                    try {
                        Imgcodecs.imwrite(str3, imread);
                        imread.release();
                        imread2.release();
                        if (i5 > picLike) {
                            EventBus.getDefault().post(new ResultBean(true, i5, picLike, (int) point.x, (int) point.y, imread2.cols(), imread2.rows()));
                        } else {
                            EventBus.getDefault().post(new ResultBean(false, i5, picLike, (int) point.x, (int) point.y, imread2.cols(), imread2.rows()));
                        }
                    } catch (Exception e2) {
                        e = e2;
                        e.printStackTrace();
                    }
                } catch (Exception e3) {
                    e = e3;
                    e.printStackTrace();
                }
            }
        } catch (Exception e4) {
            e4.printStackTrace();
        }
    }

    public void setPicLike(Context context, int i) {
        context.getSharedPreferences("VoiceData_1", 0).edit().putInt("setPicLike", i).commit();
    }

    public int getPicLike(Context context) {
        return context.getSharedPreferences("VoiceData_1", 0).getInt("setPicLike", 60);
    }

    public void run01(Context context, String str, String str2, String str3, int i) {
        Exception e;
        try {
            Log.d(TAG, "开始匹配");
            int i2 = 3;
            Mat imread = Imgcodecs.imread(str);
            Mat imread2 = Imgcodecs.imread(str2);
            System.currentTimeMillis();
            int i3 = 0;
            while (i3 < i) {
                try {
                    Mat mat = new Mat((imread.rows() - imread2.rows()) + 1, (imread.cols() - imread2.cols()) + 1, CvType.CV_32FC1);
                    Imgproc.matchTemplate(imread, imread2, mat, i2);
                    Core.normalize(mat, mat, 0.0d, 1.0d, 32, -1, new Mat());
                    Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(mat);
                    Point point = minMaxLoc.maxLoc;
                    double d = minMaxLoc.maxVal;
                    try {
                        imread = imread;
                        Imgproc.rectangle(imread, point, new Point(point.x + ((double) imread2.cols()), point.y + ((double) imread2.rows())), new Scalar(0.0d, 255.0d, 0.0d));
                        Log.d(TAG, "匹配成功：" + (d * 100.0d));
                        try {
                            Imgcodecs.imwrite(str3, imread);
                            imread.release();
                            imread2.release();
                            mat.release();
                        } catch (Exception e2) {
                            e = e2;
                        }
                    } catch (Exception e3) {
                        e = e3;
                        imread = imread;
                        e.printStackTrace();
                        i3++;
                        i2 = 3;
                    }
                } catch (Exception e4) {
                    e = e4;
                    e.printStackTrace();
                    i3++;
                    i2 = 3;
                }
                i3++;
                i2 = 3;
            }
        } catch (Exception e5) {
            e5.printStackTrace();
        }
    }
}
